import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext import db

# items that we will store in the database
class Student(db.Model):
 name  = db.StringProperty(required=True)
 home = db.StringProperty(required=True)

class MainHandler(webapp.RequestHandler):
 def get(self):

  query = Student.all()
  students = query.fetch(100)

  template_values = { 'students' : students }
  path = os.path.join(os.path.dirname(__file__), 'hello.html')
  self.response.out.write(template.render(path, template_values))

class StudentAdditionHandler(webapp.RequestHandler):
 def post(self):
  """Takes student from form's  post, enters it into db, then redirects""" 
  student_name = self.request.get('studentname')
  student_home = self.request.get('studenthome')
  student = Student(name=student_name, 
                    home=student_home)
  student.put()
  self.redirect('/')

def main():
 application = webapp.WSGIApplication([('/', MainHandler),
                                      ('/add', StudentAdditionHandler)],
                                      debug=True)
 run_wsgi_app(application)

if __name__ == '__main__':
 main()
